import {createStore,applyMiddleware} from 'redux'
import thunk from 'redux-thunk'
import Axios from 'axios'
function pusharr(arr,newarr) {
    for (let index = 0; index < arr.length; index++) {
        console.log(arr);
        
        for (let i = 0; i <arr[index].children.length; i++) {
            if(arr[index].children[i].flag===true&&arr[index].children[i].count===1){
              
                     newarr.push(arr[index].children[i])
               
               
            }
        }
    }
    
}

let reducer=( (state={list:[],allc:0,allp:0,newList:[],scount:0},action)=>{
    let newState=JSON.parse( JSON.stringify(state) )
    switch (action.type) {
        case "GET":
        newState.list=action.data
        return newState
        case "ADD":
        newState.list[action.index].children[action.i].count++
        newState.list[action.index].children[action.i].flag=true
        let ind = newState.newList.findIndex(item => item.index === action.index && item.i === action.i);
            if (ind === -1) {
                let obj = action.item;
                obj.index = action.index;
                obj.i = action.i;
                obj.count = 1;
                newState.newList.push(obj)
            } else {
                newState.newList[ind].count++;
            }
        
        return newState
        case "DEL":
        let id = newState.newList.findIndex(item => item.index === action.index && item.i === action.i);
        if(newState.list[action.index].children[action.i].count>1){
            newState.list[action.index].children[action.i].count--
            newState.newList[id].count--
        }else {
            newState.list[action.index].children[action.i].count=0
            newState.newList.splice(id,1)
        }
        return newState

        case "DEL1":
        let delIt=newState.newList[action.index]
        if(newState.newList[action.index].count>1){
            newState.newList[action.index].count--
            newState.list[delIt.index].children[delIt.i].count--
        }else{
            newState.newList.splice(action.index,1)
            newState.list[delIt.index].children[delIt.i].count=0
                }
        return newState

        case "ADD1":
        let addIt=newState.newList[action.index]
        newState.list[addIt.index].children[addIt.i].count++
        newState.newList[action.index].count++
        return newState

        case "SCOUNT":
        newState.list[action.index].scount=newState.list[action.index].children.reduce((pro,next)=>{
            return pro+next.count
        },0)

        
        return newState

        case "ACOUNT":
        newState.allc=newState.newList.reduce((pro,next)=>{
            return pro+next.count
        },0)

        
        return newState
        case "AP":
        newState.allp=newState.newList.reduce((pro,next)=>{
            return pro+next.count*next.price
        },0)

        
        return newState

        default:
           return newState
    }
} )


let store =createStore(reducer,applyMiddleware(thunk))


export default store